package com.avic.rong.activity.no;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.avic.rong.MainActivity;
import com.avic.rong.MyApplication;
import com.avic.rong.R;
import com.avic.rong.activity.login.LoginActivity;
import com.avic.rong.bean.GoCartListEvent;
import com.avic.rong.bean.HomePageEvent;
import com.avic.rong.bean.MinePageEvent;
import com.example.minato.minastore.utils.AppUtils;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CriterionListActivity extends AppCompatActivity {


    public static String ARG_URL = "arg_url";
    public static String ARG_FROM_CRITERION_LIST_ACTIVITY = "criterion_list_activity";

    @BindView(R.id.webview)
    BridgeWebView webView;

    String url = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_criterion_list);
        ButterKnife.bind(this);

        if (getIntent() != null) {
            url = getIntent().getStringExtra(ARG_URL);
        }

        if (MyApplication.getInstance().userLoginBean != null) {
//            CookieManager cookieManager = CookieManager.getInstance();
//            cookieManager.setCookie(url, "userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId()
//                    +";domain=" + AppUtils.BASE_DOMAIN + ";path=/");

            if (url.contains("?")) {
                webView.loadUrl(url + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
            } else {
                webView.loadUrl(url + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
            }
        } else {
            webView.loadUrl(url);
        }


        webView.registerHandler("pushVc", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {

                data = data.replace("\\", "");
                Logger.d(data);
                JsonObject jsonObject = (JsonObject) new JsonParser().parse(data);
                String currentUrl = jsonObject.get("url").getAsString();
                if (MyApplication.getInstance().userLoginBean != null) {
                    if (currentUrl.contains("?")){
                        currentUrl = currentUrl + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }else{
                        currentUrl = currentUrl + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId();
                    }
                }
                if (currentUrl.contains("/searchIndex")) {
                    startActivityForResult(SearchIndexActivity.queryIntent(CriterionListActivity.this, currentUrl), 100);
                }else if(currentUrl.contains("/cservice")){//进入客服
                    String tempUrl = currentUrl.replace("userIdSign","userId");
                    Intent intent = new Intent();
                    intent.setData(Uri.parse(tempUrl));
                    intent.setAction(Intent.ACTION_VIEW);
                    startActivity(intent);
//                    startActivity(IMActivity.queryIntent(CriterionListActivity.this,currentUrl.replace("userIdSign","userId")));
                } else {
                    if (currentUrl.contains("/good/") || currentUrl.contains("goods/goodsInfo")){
                        if (!currentUrl.contains("/app")){
                            if (currentUrl.contains("goods/goodsInfo")){
                                currentUrl = currentUrl.replace("goods/goodsInfo","app/good/info");
                            }else if(currentUrl.contains("good/info")){
                                currentUrl = currentUrl.replace("good/info","app/good/info");
                            }
                        }else{
                            if (currentUrl.contains("goods/goodsInfo")){
                                currentUrl = currentUrl.replace("goods/goodsInfo","good/info");
                            }
                        }
                    }
                    startActivity(CriterionDetailActivity.queryIntent(CriterionListActivity.this, currentUrl));
                }
            }
        });


        webView.registerHandler("goLogin", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                startActivityForResult(LoginActivity.getIntent(CriterionListActivity.this, ARG_FROM_CRITERION_LIST_ACTIVITY), 1002);
            }
        });

        //去购物车
        webView.registerHandler("goCartList", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new GoCartListEvent());
                Intent intent = MainActivity.queryIntent(CriterionListActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        webView.registerHandler("goBack", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                finish();
            }
        });


        //首页
        webView.registerHandler("homeClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new HomePageEvent());
                Intent intent = MainActivity.queryIntent(CriterionListActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        //搜索
        webView.registerHandler("searchClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                startActivityForResult(SearchIndexActivity.queryIntent(CriterionListActivity.this, AppUtils.BASE_URL + "app/searchIndex"),100);
            }
        });
        //我的融融
        webView.registerHandler("memberClick", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().postSticky(new MinePageEvent());
                Intent intent = MainActivity.queryIntent(CriterionListActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 100) {
            String currentUrl = data.getStringExtra("url");
            if (MyApplication.getInstance().userLoginBean != null && !currentUrl.contains("userIdSign")) {
                if (currentUrl.contains("?")) {
                    webView.loadUrl(currentUrl + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
                } else {
                    webView.loadUrl(currentUrl + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
                }
            } else {
                webView.loadUrl(currentUrl);
            }
        } else if (requestCode == 1002) {//登录come resource criterion list activity
            if (MyApplication.getInstance().userLoginBean != null) {
                if (url.contains("?")) {
                    webView.loadUrl(url + "&userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
                } else {
                    webView.loadUrl(url + "?userIdSign=" + MyApplication.getInstance().userLoginBean.getUserId());
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().removeAllStickyEvents();
    }

    public static Intent queryIntent(Context context, String url) {
        Intent intent = new Intent(context, CriterionListActivity.class);
        intent.putExtra(ARG_URL, url);
        return intent;
    }


}
